import java.io.*;
import java.util.HashSet;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RequiresPermissionsScanner {

    private static final Pattern PERMISSIONS_PATTERN = Pattern.compile("@RequiresPermissions\\(\\\"([^\"]+)\\\"\\)");
    private Set<String> permissions = new HashSet<>();

    public void scanDirectory(File directory) {
        if (!directory.exists() || !directory.isDirectory()) {
            System.err.println("指定路径不是一个有效目录: " + directory.getAbsolutePath());
            return;
        }

        scanFiles(directory);
    }

    private void scanFiles(File file) {
        if (file.isDirectory()) {
            File[] files = file.listFiles();
            if (files != null) {
                for (File f : files) {
                    scanFiles(f);
                }
            }
        } else if (file.isFile() && file.getName().endsWith(".java")) {
            scanJavaFile(file);
        }
    }

    private void scanJavaFile(File javaFile) {
        try (BufferedReader reader = new BufferedReader(new FileReader(javaFile))) {
            String line;
            while ((line = reader.readLine()) != null) {
                Matcher matcher = PERMISSIONS_PATTERN.matcher(line);
                while (matcher.find()) {
                    String permission = matcher.group(1);
                    permissions.add(permission);
                }
            }
        } catch (IOException e) {
            System.err.println("读取文件时出错: " + javaFile.getAbsolutePath() + " - " + e.getMessage());
        }
    }

    public Set<String> getPermissions() {
        return new HashSet<>(permissions);
    }

    // 新增方法：将结果保存到文件
    public void savePermissionsToFile(String outputPath) throws IOException {
        try (PrintWriter writer = new PrintWriter(new FileWriter(outputPath))) {
            writer.println("找到以下 @RequiresPermissions 权限值:");
            permissions.stream()
                    .sorted()
                    .forEach(permission -> writer.println("- " + permission));
            writer.println();
            writer.println("总共找到 " + permissions.size() + " 个权限值");
        }
    }

    public void printPermissions() {
        System.out.println("找到以下 @RequiresPermissions 权限值:");
        permissions.stream()
                .sorted()
                .forEach(permission -> System.out.println("- " + permission));
        System.out.println("\n总共找到 " + permissions.size() + " 个权限值");
    }

    public static void main(String[] args) {
        if (args.length < 1) {
            System.err.println("用法: java RequiresPermissionsScanner <目录路径> [输出文件路径]");
            System.exit(1);
        }

        String directoryPath = args[0];
        String outputPath = args.length > 1 ? args[1] : null;

        File directory = new File(directoryPath);

        RequiresPermissionsScanner scanner = new RequiresPermissionsScanner();
        scanner.scanDirectory(directory);
        scanner.printPermissions();

        // 如果指定了输出文件路径，则保存结果到文件
        if (outputPath != null) {
            try {
                scanner.savePermissionsToFile(outputPath);
                System.out.println("\n结果已保存到文件: " + outputPath);
            } catch (IOException e) {
                System.err.println("保存文件时出错: " + e.getMessage());
            }
        }
    }
}
